package com.huadao.cart_ccs.experiment.log.paramsAnysis;

import android.graphics.Color;

import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.huadao.cart_ccs.R;
import com.huadao.cart_ccs.common.ProjectManager;
import com.huadao.cart_ccs.common.UtilHelpers;
import com.huadao.cart_ccs.data.bo.ExperimentStage;
import com.huadao.cart_ccs.greendao.GreenDaoManager;
import com.huadao.cart_ccs.greendao.entity.CO2;
import com.huadao.cart_ccs.greendao.entity.O2;
import com.huadao.cart_ccs.greendao.entity.Temperature;
import com.huadao.cart_ccs.greendao.gen.CO2Dao;
import com.huadao.cart_ccs.greendao.gen.ExperimentStageDao;
import com.huadao.cart_ccs.greendao.gen.O2Dao;
import com.huadao.cart_ccs.greendao.gen.TemperatureDao;
import com.huadao.cart_ccs.main.CartApplication;

import java.util.List;

public class ParamsAnysisPresenter extends IParamsAnysisContract.IParamsAnysisPresenter {

    private CO2Dao co2Dao;
    private O2Dao o2Dao;
    private TemperatureDao temperatureDao;
    private ExperimentStageDao experimentStageDao;

    private LineDataSet dataSetCO2;
    private LineDataSet dataSetO2;
    private LineDataSet dataSetTemperature;

    private long projectId;
    private long stageId = -1;
    private int day = 1;

    public ParamsAnysisPresenter(IParamsAnysisContract.IParamsAnysisView baseView) {
        attachView(baseView);
        co2Dao = GreenDaoManager.getInstance().getNoCacheSession().getCO2Dao();
        o2Dao = GreenDaoManager.getInstance().getNoCacheSession().getO2Dao();
        temperatureDao = GreenDaoManager.getInstance().getNoCacheSession().getTemperatureDao();
        projectId = ProjectManager.getInstance().getProjectId();

        experimentStageDao = GreenDaoManager.getInstance().getNoCacheSession().getExperimentStageDao();
    }

    @Override
    LineData getTemperatureData() {
        if (dataSetTemperature == null) {
            dataSetTemperature = createLineDataSet(CartApplication.getInstance().getString(R.string.temperature));
        } else {
            dataSetTemperature.clear();
        }
        List<Temperature> temperatureList = temperatureDao.queryBuilder().where(TemperatureDao.Properties.ProjectId.eq(projectId), TemperatureDao.Properties.StageId.eq(stageId)).list();
        for (int i = 0; i < temperatureList.size(); i++) {
            Temperature temperature = temperatureList.get(i);
            float x = UtilHelpers.getTime(temperature.getDate());
            Entry entry = new Entry(x, (float) temperature.getTemperature());
            dataSetTemperature.addEntry(entry);
        }
        LineData lineDataTemperature = new LineData(dataSetTemperature);
        return lineDataTemperature;
    }

    @Override
    LineData getCo2Data() {
        if (dataSetCO2 == null) {
            dataSetCO2 = createLineDataSet(CartApplication.getInstance().getString(R.string.CO2));
        } else {
            dataSetCO2.clear();
        }

        List<CO2> CO2List = co2Dao.queryBuilder().where(CO2Dao.Properties.ProjectId.eq(projectId), CO2Dao.Properties.StageId.eq(stageId)).list();
        for (int i = 0; i < CO2List.size(); i++) {
            CO2 co2 = CO2List.get(i);
            float x = UtilHelpers.getTime(co2.getDate());
            Entry entry = new Entry(x, (float) co2.getCo2());
            dataSetCO2.addEntry(entry);
        }
        LineData lineDataCO2 = new LineData(dataSetCO2);
        return lineDataCO2;
    }

    @Override
    LineData getO2Data() {
        if (dataSetO2 == null) {
            dataSetO2 = createLineDataSet(CartApplication.getInstance().getString(R.string.O2));
        } else {
            dataSetO2.clear();
        }
        List<O2> O2List = o2Dao.queryBuilder().where(O2Dao.Properties.ProjectId.eq(projectId), O2Dao.Properties.StageId.eq(stageId)).list();
        for (int i = 0; i < O2List.size(); i++) {
            O2 o2 = O2List.get(i);
            float x = UtilHelpers.getTime(o2.getDate());
            Entry entry = new Entry(x, (float) o2.getO2());
            dataSetO2.addEntry(entry);
        }
        LineData lineDataO2 = new LineData(dataSetO2);
        return lineDataO2;
    }

    private LineDataSet createLineDataSet(String label) {
        LineDataSet set = new LineDataSet(null, label);
        set.setMode(LineDataSet.Mode.HORIZONTAL_BEZIER);
        set.setAxisDependency(YAxis.AxisDependency.LEFT);

        // 折线的颜色
        set.setColor(CartApplication.getInstance().getResources().getColor(R.color.temperature_line));

        set.setCircleColor(Color.WHITE);
        set.setLineWidth(5f);
        set.setCircleRadius(2f);
        set.setFillAlpha(128);
//        set.setFillColor(R.color.temperature_line);
        set.setHighLightColor(CartApplication.getInstance().getResources().getColor(R.color.temperature_line));
//        set.setValueTextColor(Color.WHITE);
//        set.setValueTextSize(10f);
        set.setDrawValues(false);
        return set;
    }

    @Override
    int getDay() {
        return day;
    }

    @Override
    void setDay(int day) {
        this.day = day;
        initStageId();
    }

    private void initStageId() {
        List<ExperimentStage> experimentStageList = experimentStageDao.queryBuilder().where(ExperimentStageDao.Properties.ProjectId.eq(projectId), ExperimentStageDao.Properties.Day.eq(day)).list();
        if (experimentStageList != null && experimentStageList.size() > 0) {
            stageId = experimentStageList.get(0).getId();
        } else {
            stageId = -1;
        }
    }

}
